package com.bawei.rk_01

import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.base.utils.mvvm.BaseAcitivity
import com.bawei.rk_01.Adapter.GoodsAdapter
import com.bawei.rk_01.Home.SearchViewModel
import com.bawei.rk_01.Room.AppDBUtils
import com.bawei.rk_01.Room.SearchEntity
import com.bawei.rk_01.databinding.ActivitySearchBinding
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.android.material.chip.Chip

@Route(path = "/app/SearchActivity")

class SearchActivity : BaseAcitivity<ActivitySearchBinding,SearchViewModel>() {

    lateinit var goodsAdapter: GoodsAdapter

    override fun getViewModel(): Class<SearchViewModel> {
        return SearchViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_search
    }

    override fun initView() {

        goodsAdapter = GoodsAdapter()
        binding.rvGoods.layoutManager = GridLayoutManager(this,2)
        binding.rvGoods.adapter = goodsAdapter

        //搜索实现
        binding.search.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query!=null){
                    viewModel.search(0,query,20,false,1,"","")
                }
                AppDBUtils.getDB().searchDao().insert(SearchEntity(query.toString(), SPUtils.getInstance().getString("phone")))
                querySearch()
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText!=null){
                    viewModel.search(0,newText,20,false,1,"","")
                }else{
                    goodsAdapter.data.clear()
                    goodsAdapter.notifyDataSetChanged()
                }
                return false
            }

        })

        //多个触发事件
        querySearch()
        //点击删除删除图标，删除所有
        binding.del.setOnClickListener {
            AppDBUtils.getDB().searchDao().deleteAll(SPUtils.getInstance().getString("phone"))
            binding.cg.removeAllViews()
        }

    }

    //多个触发事件
    fun querySearch(){
        val query = AppDBUtils.getDB().searchDao().query(SPUtils.getInstance().getString("phone")).also {
            //倒序
            it.reverse()
            //只显示五条数据
            if (it.size > 5){
                for (i in 5 until it.size){
                    it.removeLast()
                }
            }
        }
        if (query.isNotEmpty()){
            //清空其他控件
            binding.cg.removeAllViews()
            query.forEach {
                //找到chip控件
                val chip = LayoutInflater.from(this).inflate(R.layout.item_chip,null) as Chip
                //动态设置chip内容
                chip.findViewById<TextView>(R.id.keywoed).text = it.keywoed
                //将chip视图添加到chipGroup中
                binding.cg.addView(chip)
                //点击itme，实现查询
                chip.setOnClickListener { it1->
                    binding.search.setQuery(it.keywoed,true)
                }
                //点击删除图标，实现删除
                chip.setOnCloseIconClickListener { it2 ->
                    //先删后台
                    AppDBUtils.getDB().searchDao().delete(it)
                    //在删页面
                    binding.cg.removeView(it2)
                }
            }
        }
    }

    override fun initData() {

        viewModel.searchsuccess.observe(this){
            if (it.code == 200){
                goodsAdapter.data.clear()
                goodsAdapter.setList(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong(it.message)
            }
        }

    }

}